package com.bicon.botu.mall.order.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.core.base.common.BusinessException;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.order.dto.*;
import com.bicon.botu.mall.order.service.IAfterSalesRecordsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单退款退货记录 前端控制器
 * </p>
 *
 * @author 李双喜
 * @since 2018-06-14
 */
@RestController
@RequestMapping("/V3.0.0/after-sales-records")
public class AfterSalesRecordsController extends BaseController {

    @Autowired
    private IAfterSalesRecordsService afterSalesRecordsService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public BaseResult<Page<AfterSalesItemDto>> getAfterSalesByCondition(@RequestParam(required = false) String queryType,
                                                                        @RequestParam(required = false) String keyWord,
                                                                        @RequestParam(required = false) String sortType,
                                                                        @RequestParam(required = false) String sortDirection,
                                                                        @RequestParam Map<String, String> params,
                                                                        @RequestParam(defaultValue = "0") Integer pageNo,
                                                                        @RequestParam(defaultValue = "10") Integer pageSize) {
        if (params != null && !params.isEmpty() && params.containsKey("storeId")) {
            if (StringUtils.isNoneBlank(queryType, keyWord)) {
                params.put(queryType, keyWord);
            }
            Page<AfterSalesItemDto> page = new Page<AfterSalesItemDto>(pageNo, pageSize);
            if (StringUtils.isNotBlank(sortType)) {
                page.setOrderByField(sortType);
                page.setAsc("ASC".equals(sortDirection) ? true : false);
            } else {
                page.setOrderByField("create_date");
                page.setAsc(false);
            }
            Page<AfterSalesItemDto> resultPage = afterSalesRecordsService.getAfterSalesPageByCondition(page, params);
            return sendResult200(resultPage);
        } else {
            return sendResult400();
        }
    }

    @RequestMapping(value = "export", method = RequestMethod.GET)
    public void exportRecords(@RequestParam(required = false) String queryType,
                                @RequestParam(required = false) String keyWord,
                                @RequestParam Map<String, String> params,
                                HttpServletRequest request,
                                HttpServletResponse response) {
        if (params != null && !params.isEmpty() && params.containsKey("storeId")) {
            if (StringUtils.isNoneBlank(queryType, keyWord)) {
                params.put(queryType, keyWord);
            }
            afterSalesRecordsService.exportRecords(params,request,response);
        } else {
            throw new BusinessException(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400);
        }
    }


    @RequestMapping(value = "/statistic", method = RequestMethod.GET)
    public BaseResult<List<AfterSalesStatisticsDto>> statisticAfterSale(@RequestParam("storeId") String storeId) {
        return this.sendResult200(this.afterSalesRecordsService.statisticAfterSale(storeId));
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public BaseResult<AfterSalesForWebDto> queryAfterSalesRecords(@RequestParam("afterSalesId") String afterSalesId) {
        return this.sendResult200(this.afterSalesRecordsService.queryAfterSalesRecords(afterSalesId));
    }

    @RequestMapping(value = "/goods-list", method = RequestMethod.GET)
    public BaseResult<Page<AfterSalesDetailsDto>> queryAfterSalesGoodsList(@RequestParam("afterSalesId") String afterSalesId,
                                                                           @RequestParam(defaultValue = "0") Integer pageNo,
                                                                           @RequestParam(defaultValue = "10") Integer pageSize) {
        return this.sendResult200(this.afterSalesRecordsService.queryAfterSalesGoodsList(afterSalesId,pageNo,pageSize));
    }

    @RequestMapping(value = "/approval", method = RequestMethod.POST)
    public BaseResult approvalAfterSales(@RequestParam(value = "afterSalesId") String afterSalesId,
                                         @RequestParam(value = "approvalResult") boolean approvalResult,
                                         @RequestParam(value = "payBackFreight", required = false) boolean payBackFreight,
                                         @RequestParam(value = "payBackAmount", required = false) BigDecimal payBackAmount,
                                         @RequestParam(value = "deliveryPointId", required = false) String deliveryPointId,
                                         @RequestParam(value = "backDeliveryMethod", required = false) Integer backDeliveryMethod,
                                         @RequestParam(value = "rejectReason", required = false) String rejectReason) {
        String userId="";
        //String userId = this.getUserId();
        return this.sendResult200(this.afterSalesRecordsService.approvalAfterSales(afterSalesId, approvalResult, payBackFreight,
                payBackAmount, deliveryPointId, backDeliveryMethod, rejectReason,userId));
    }

    @RequestMapping(value = "/receivedGoods", method = RequestMethod.POST)
    public BaseResult receivedGoods(@RequestParam(value = "afterSalesId") String afterSalesId){
        String userId="";
        //String userId = this.getUserId();
        return this.sendResult200(this.afterSalesRecordsService.receivedGoods(userId,afterSalesId));
    }

}
